package com.example.widgetcase.slice.colorfilter;

import com.example.widgetcase.ResourceTable;
import com.example.widgetcase.slice.BaseSlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.Slider;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.ColorMatrix;

public class ColorHueAbility extends BaseSlice implements Slider.ValueChangedListener {

    private Image jingyHueIv;
    private Slider bar_Hue;
    private Slider bar_Saturation;
    private Slider bar_Lightness;
    private ColorMatrix mHueMatrix;
    private ColorMatrix colorMatrix;
    private ColorMatrix mSaturationMatrix;
    private ColorMatrix mLightnessMatrix;

    @Override
    public int getChildUIContent() {
        return ResourceTable.Layout_color_hue;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        jingyHueIv = (Image) findComponentById(ResourceTable.Id_jingyHueIv);
        bar_Hue = (Slider) findComponentById(ResourceTable.Id_bar_Hue);
        bar_Saturation = (Slider) findComponentById(ResourceTable.Id_bar_Saturation);
        bar_Lightness = (Slider) findComponentById(ResourceTable.Id_bar_Lightness);
        bar_Hue.setValueChangedListener(this);
        bar_Saturation.setValueChangedListener(this);
        bar_Lightness.setValueChangedListener(this);

        colorMatrix = new ColorMatrix();
        mHueMatrix = new ColorMatrix();
        mSaturationMatrix = new ColorMatrix();
        mLightnessMatrix = new ColorMatrix();

        PixelMapElement element = new PixelMapElement(jingyHueIv.getPixelMap());
        jingyHueIv.setImageElement(element);
    }

    @Override
    public void onProgressUpdated(Slider slider, int i, boolean b) {
        float hueValue = (bar_Hue.getProgress() - 128f) * 1.0f / 128f * 180;
        float saturationValue = bar_Saturation.getProgress() / 128f;
        float lightnessValue = bar_Lightness.getProgress() / 128f;

        // 设置色相
        mHueMatrix.reset();
        //TODO setRotate没有对应的ohos实现
        mHueMatrix.setSaturation(hueValue); //临时用一下
//        mHueMatrix.setRotate(0, hueValue);
//        mHueMatrix.setRotate(1, hueValue);
//        mHueMatrix.setRotate(2, hueValue);

        // 设置饱和度
        mSaturationMatrix.reset();
        mSaturationMatrix.setSaturation(saturationValue);

        // 亮度
        mLightnessMatrix.reset();
        mLightnessMatrix.setScale(lightnessValue, lightnessValue, lightnessValue, 1f);

        // 效果叠加
        colorMatrix.reset();
        colorMatrix.postConcat(mHueMatrix);
        colorMatrix.postConcat(mSaturationMatrix);
        colorMatrix.postConcat(mLightnessMatrix);

        jingyHueIv.getImageElement().setColorMatrix(colorMatrix);
    }

    @Override
    public void onTouchStart(Slider slider) {

    }

    @Override
    public void onTouchEnd(Slider slider) {

    }
}
